import { getprepareLessonDetail, prepareLessonCollect, prepareLessonCancelCollect } from '../../../api/prepareLesson.js'
import { tipOffCommit } from '../../../api/tipOff'
import { formatTime } from '../../../utils/util'

const no2str = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十']

Page({
  data: {
    loading: true,
    activityId: '', //活动ID
    activityType: '', //活动ID
    activeProgramTab: 0, // 方案tab
    activePeriodTab: 0, // 课时tab
    activeClassifyTab: 0, //分类tab
    createTime: '',
    baseInfoData: {},
    resListData: [],
    periodData: {},
    programData: {},

    showReportType: false,
    reportTypes: [
      { name: '盗版侵权', value: '盗版侵权'  },
      { name: '广告或垃圾信息', value: '广告或垃圾信息'  },
      { name: '色情，淫秽，低俗信息', value: '色情，淫秽，低俗信息'  },
      { name: '反人类，反社会，反政府等信息', value: '反人类，反社会，反政府等信息'  }
    ]
  },
  onLoad: function (options) {
    this.setData({
      activityId: options.activityId
    })
    this.init(this.data.activityId)
  },
  options: {
    styleIsolation: 'shared'
  },
  init(activityId) {
    this.getprepareLessonDetail(activityId)
  },
  async getprepareLessonDetail(activityId) {
    const _this = this
    const params = {
      id: activityId
    }
    let res = await getprepareLessonDetail(params)

    res.resList.forEach((list, index) => {
      if (!list.TeachingDesignName) {
        list.TeachingDesignName = '方案' + no2str[index]
      }
      list.createTime = formatTime(list.createTime, '{y}.{m}.{d}')
    })

    this.setData({
      baseInfoData: res.baseInfo,
      resListData: res.resList,
      programData: res.resList[this.data.activeProgramTab],
      periodData: res.resList[this.data.activeProgramTab].resList[this.data.activePeriodTab],
      loading: false
    })

    if (res.resList.length > 0) {
      this.setData({
        activeProgramTab: 0,
        activePeriodTab: 0,
        activeClassifyTab: 0,
        loading: false
      })
    }
  },
  onChangeProgramTab(event) {
    let _this = this
    console.log(event)
    this.setData({
      programData: _this.data.resListData[event.detail.name],
      periodData: _this.data.resListData[event.detail.name].resList[0],
      activePeriodTab: 0,
      activeClassifyTab: 0,
      activeProgramTab: event.detail.index
    })
  },
  onClickPeriodTab(event) {
    let _this = this
    this.setData({
      periodData: _this.data.programData[event.detail.name],
      activeClassifyTab: 0
    })
  },
  async onClickStar() {
    let _this = this
    if (_this.data.programData.isCollect) {
      const params = {
        id: _this.data.programData.id
      }
      await prepareLessonCancelCollect(params)
      const isCollect = 'resListData[' + this.data.activeProgramTab + '].isCollect'
      _this.setData({
        'programData.isCollect': false,
        [isCollect]: false
      })
    } else {
      const params = {
        id: _this.data.programData.id
      }
      await prepareLessonCollect(params)
      const isCollect = 'resListData[' + this.data.activeProgramTab + '].isCollect'
      _this.setData({
        'programData.isCollect': true,
        [isCollect]: true
      })
    }
  },
  //点击举报
  onClickInform(){
    this.setData({
      showReportType: true
    })
  },
  //隐藏举报列表
  cancelReportType() {
    this.setData({
      showReportType: false
    })
  },
  // 提交举报
  async confirmReportType(e){
    let params = {
      content: e.detail.name,
      targetType: 'pre_lesson',
      targetId: this.data.activityId,
      targetUserId: this.data.baseInfoData.user.userId,
      targetName: this.data.baseInfoData.title
    }
    await tipOffCommit(params)
  },
})
